package team.bailan.common.process.dealcard;

import lombok.Data;
import team.bailan.entity.domain.Card;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/*
发牌
 */
@Data
public class DealCards {
    List<Card> allCards;
    List<Card> player1Cards = new ArrayList<>();
    List<Card> player2Cards = new ArrayList<>();
    List<Card> player3Cards = new ArrayList<>();
    List<Card> lordCards = new ArrayList<>();

    // 生成一组牌
    public List<Card> generateCards() {
        List<Card> newCards = new ArrayList<>();
        for (int i = 1; i <= 2; i++) {
            for (int j = 1; j <=4; j++) {
                Card card = new Card();
                card.setDecor(j);
                card.setCardFace(i);
                card.setValue(i+11);
                newCards.add(card);
            }
        }
        for (int i = 3; i <= 13; i++) {
            for (int j = 1; j <=4; j++) {
                Card card = new Card();
                card.setDecor(j);
                card.setCardFace(i);
                card.setValue(i - 2);
                newCards.add(card);
            }
        }
        newCards.add(new Card(0, 14, 14));
        newCards.add(new Card(0, 15, 15));
        return newCards;
    }

    // 初始化牌
    public void init() {
        player1Cards = new ArrayList<>();
        player2Cards = new ArrayList<>();
        player3Cards = new ArrayList<>();
        lordCards = new ArrayList<>();
        allCards = new ArrayList<>();
        allCards = generateCards();
    }
    // 洗牌
    public void shuffleCards() {
        init();
        Collections.shuffle(allCards);
    }

    // 纸牌排序
    public static void cardsSort(List<Card> cards) {
        cards.sort(new Comparator<Card>() {
            @Override
            public int compare(Card o1, Card o2) {
                if (o1.getValue() == o2.getValue()) {
                    return o1.getDecor() - o2.getDecor();
                }
                return o1.getValue() - o2.getValue();
            }
        });
    }
    
    // 分牌
    public void dealCards() {
        player1Cards.addAll(allCards.subList(0, 17));
        player2Cards.addAll(allCards.subList(17, 34));
        player3Cards.addAll(allCards.subList(34, 51));
        lordCards.addAll(allCards.subList(51, 54));
        cardsSort(player1Cards);
        cardsSort(player2Cards);
        cardsSort(player3Cards);

    }

    // 发牌全过程
    public void dealCardsProcess() {
        shuffleCards();
        dealCards();
    }

    // 获取农民的牌
    public List<Card> getCardsByPlayerPosition(int playerPosition) {
        if (playerPosition == 1) {
            return this.player1Cards;
        } else if (playerPosition == 2) {
            return this.player2Cards;
        } else {
            return this.player3Cards;
        }
    }

    // 获取地主的三张牌
    public List<Card> getLordCards() {
        return this.lordCards;
    }

    // 地主获得地主牌
    public void addLordCardsToPlayer(int playerPosition) {
        if (playerPosition == 1) {
            player1Cards.addAll(lordCards);
            cardsSort(player1Cards);
        } else if (playerPosition == 2) {
            player2Cards.addAll(lordCards);
            cardsSort(player2Cards);
        } else {
            player3Cards.addAll(lordCards);
            cardsSort(player3Cards);
        }
    }
}
